package com.itedge.solutionmanager.web.controller.task.impl;

import javax.annotation.PostConstruct;

import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.itedge.infrastructure.domain.IProcessEntity;
import com.itedge.infrastructure.service.domain.IProcessEntityService;
import com.itedge.infrastructure.service.process.IProcessService;
import com.itedge.infrastructure.web.controller.task.impl.AbstractTaskController;
import com.itedge.infrastructure.web.taskhandler.ITaskDataHandler;
import com.itedge.solutionmanager.domain.impl.Solution;
import com.itedge.solutionmanager.taskdata.impl.solution.AddDocumentationTaskData;
import com.itedge.solutionmanager.taskdata.impl.solution.ArchiveMeetingOutputTaskData;
import com.itedge.solutionmanager.taskdata.impl.solution.RegisterCustomerTaskData;
import com.itedge.solutionmanager.taskdata.impl.solution.ScheduleMeetingTaskData;
import com.itedge.solutionmanager.taskdata.impl.solution.SolutionDeliveryTaskData;
import com.itedge.solutionmanager.taskdata.impl.solution.SolutionDesignTaskData;

/**
 * Implementation of task controller for {@link Solution} domain object
 * 
 * @author jhe
 *
 */
@RequestMapping("/solutionTasks")
@Controller
public class SolutionTaskController extends AbstractTaskController {

	@Autowired
    private IProcessService<Solution> solutionProcessService;	
    @Autowired
    private IProcessEntityService<Solution> solutionService;   	
    @Autowired
    private ITaskDataHandler<AddDocumentationTaskData, Solution> addDocumentationTaskDataHandler;
    @Autowired
    private ITaskDataHandler<ArchiveMeetingOutputTaskData, Solution> archiveMeetingOutputTaskDataHandler;
    @Autowired
    private ITaskDataHandler<RegisterCustomerTaskData, Solution> registerCustomerTaskDataHandler;
    @Autowired
    private ITaskDataHandler<ScheduleMeetingTaskData, Solution> scheduleMeetingTaskDataHandler;
    @Autowired
    private ITaskDataHandler<SolutionDeliveryTaskData, Solution> solutionDeliveryTaskDataHandler;
    @Autowired
    private ITaskDataHandler<SolutionDesignTaskData, Solution> solutionDesignTaskDataHandler;
	
	private static final String ADD_DOCUMENTATION_TASK = "ADD_DOCUMENTATION";
	private static final String ARCHIVE_MEET_OUTPUT_TASK = "ARCHIVE_MEETING_OUTPUT";
	private static final String REGISTER_CUSTOMER_TASK = "REGISTER_CUSTOMER";
	private static final String SCHEDULE_MEET_TASK = "SCHEDULE_MEETING";
	private static final String SOLUTION_DELIVERY_TASK = "SOLUTION_DELIVERY";
	private static final String SOLUTION_DESIGN_TASK = "SOLUTION_DESIGN";
	
	@Autowired
    protected SolutionTaskController(MessageSource messageSource) {
		super(messageSource);
	}
	
	@Override
	@PostConstruct
	public void initializeMappings() {
		taskHandlerMapping.put(ADD_DOCUMENTATION_TASK, addDocumentationTaskDataHandler);		
		taskHandlerMapping.put(ARCHIVE_MEET_OUTPUT_TASK, archiveMeetingOutputTaskDataHandler);	
		taskHandlerMapping.put(REGISTER_CUSTOMER_TASK, registerCustomerTaskDataHandler);
		taskHandlerMapping.put(SCHEDULE_MEET_TASK, scheduleMeetingTaskDataHandler);
		taskHandlerMapping.put(SOLUTION_DELIVERY_TASK, solutionDeliveryTaskDataHandler);	
		taskHandlerMapping.put(SOLUTION_DESIGN_TASK, solutionDesignTaskDataHandler);	
	}

	@Override
	protected Task getCurrentTask(String id) {
		return solutionProcessService.getTaskById(id);
	}

	@Override
	protected HistoricTaskInstance getHistoryTask(String id) {
		return solutionProcessService.getHistoryTaskById(id);
	}

	@Override
	protected IProcessEntity getLinkedObject(String procId) {
		return solutionService.findEntityByProcessId(procId);
	}

	@Override
	protected String getTaskViewPath() {
		return "solutionTasks/";
	}

	@Override
	protected String getLinkedObjectViewPath() {
		return "solutions/";
	}

	@Override
	protected String getLinkedObjectAttrName() {
		return "solution";
	}
    
}
